<?php
/**
* Copyright (c) 2002-2007 BioWare Corp.
* http://www.bigdaddymonitor.org
*
* This file is part of the BigDaddy Monitoring
* program. It is free software distributed under
* the terms of the BSD License. It is ditributed
* in the hopes that it will be useful, but
* WITHOUT ANY WARRANTY; without event the implied
* warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE.
*
* The BSD License is available from:
* http://www.opensource.org/licenses/bsd-license.php
**/
class ParseIni
{

    var $iniFile;
    var $iniArray;
    var $iniID;

    function ParseIni($iniID=NULL)
    {
        if ($iniID!="")
        {
            $this->iniID=$iniID;
            $this->getfromDB();
        }
    }

    /*===========================================================================================*/
    //array read_inifile(str IniFile)
    //Reads an initialization file and strips it into variables
    //DW 17.03.2004
    /*===========================================================================================*/
    
    function read_inifile()
    {
        $iniArray=explode("\n",$this->iniFile);
        
        //First split the entire file into groups
        
        $currentSection="";
        
        foreach ($iniArray as $iniLine)
        {
            //First find out if this is a new section, if so set the c
            
            if (preg_match("#^\[(.*)\]#i", $iniLine, $matches))
            {
                $currentSection=$matches[1];
            }
            elseif (preg_match("#(.*?)=(.*)#i", $iniLine, $matches))
            {
                if ($currentSection!="")
                {
                    $returnArray[$currentSection][trim($matches[1])]=trim($matches[2]);
                }
                else
                {
                    $returnArray[trim($matches[1])]=trim($matches[2]);
                }
            }
        }
        
        $this->iniArray=$returnArray;
        return $returnArray;
    }
    
    /*===========================================================================================*/
    //str write_inivar(str IniVar, str iniFile)
    //Attempts to insert/update a value in an inifile
    //DW 17.03.2004
    /*===========================================================================================*/
    function write_inivar($iniVar=NULL, $iniFile=NULL)
    {
        //IniVar should be formatted thus:
        //[level]variable=value
        //[level] is optional
        if ($iniFile=="" && $this->iniFile!="")
        {
            $iniFile=$this->iniFile;
        }
        
        $this->read_inifile($iniFile);
        if (preg_match("#^\[(.*)\](.*?)=(.*)#", $iniVar, $matches))
        {
            $this->iniArray[trim($matches[1])][trim($matches[2])]=trim($matches[3]);
        }
        elseif (preg_match("#^(.*?)=(.*)#", $iniVar, $matches))
        {
            $this->iniArray[trim($matches[1])]=trim($matches[2]);
        }
        
        
        $this->iniFile=$this->arrayToIni();
        return $this->iniFile;
        
    }
    

    /*===========================================================================================*/
    //str arrayToIni(array $iniArray)
    //Takes an array (max 2 dimensions) and creates an inifile out of it
    //DW 17.03.2004
    /*===========================================================================================*/

    function arrayToIni($iniArray=NULL)
    {
        if ($iniArray=="")
        {
            $iniArray=$this->iniArray;
        }
        
        array_multisort($iniArray, SORT_REGULAR);
        
        $keysArray=array_keys($iniArray);
        
    
        if (count($keysArray)>0)
        {
            foreach ($keysArray as $key)
            {
                
                if (is_array($iniArray[$key]))
                {
                    $currentLevel=$key;
                    $iniStr.="[".$key."]\n";
                    
                    foreach($iniArray[$key] as $secondarykey => $value)
                    {
                        $iniStr.=$secondarykey."=".$value."\n";
                    }
                    
                }
                else
                {
                    $iniStr.=$key."=".$iniArray[$key]."\n";
                }
            }
        }
        
        return $iniStr;
        
    }
    
    /*===========================================================================================*/
    //str writetoDB([str ID, [str iniFile]])
    //Writes the current ini file to the database based on the String ID
    //DW 18.03.2004
    /*===========================================================================================*/

    function writetoDB($ID=NULL, $iniFile=NULL)
    {
        if ($iniFile=="")
        {
            $iniFile=$this->iniFile;
        }
        
        if ($ID=="")
        {
            $ID=$this->iniID;
        }
        
        $sql="UPDATE inifile SET inifile='".$iniFile."' WHERE ID='".$ID."'";
        $result=run_mysql_query($sql);
    }
    
    /*===========================================================================================*/
    //str getfromDB(str ID)
    //Writes the current ini file to the database based on the String ID
    //DW 18.03.2004
    /*===========================================================================================*/

    function getfromDB($ID=NULL)
    {
        if ($ID=="")
        {
            $ID=$this->iniID;
        }
        
        $sql="SELECT inifile FROM inifile WHERE ID='".$ID."'";
        $result=mysql_fetch_array(run_mysql_query($sql));
        
        $this->iniFile=$result["inifile"];
        return $result["inifile"];
    }

}

?>
